package controller

import (
	"github.com/gofiber/fiber"
	"gowork/distributionRuleManagement/model"
	"strconv"
)

type DistributionRuleManagementController struct {
	DistributionRuleManagement *model.DistributionRuleManagement
}

func (drmc *DistributionRuleManagementController)ToGrantAuthorization(c *fiber.Ctx){

	drmc.DistributionRuleManagement = new(model.DistributionRuleManagement)

	nutritionistIdStr := c.FormValue("nutritionistId")
	nutritionistId, err := strconv.ParseInt(nutritionistIdStr,10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	random := c.FormValue("random")

	res , err := drmc.DistributionRuleManagement.ToGrantAuthorization(nutritionistId, random)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}